home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / resolution.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  5.3 KB  |  265 lines

  1.  
  2. /* resolution.c */
  3.  
  4. /* by Walter Vannini (walterv@jps.net, waltervannini@hotmail.com) */
  5.  
  6. /* Copyright (c) Walter Vannini, 1998.  */
  7.  
  8. /* This program is freely distributable without licensing fees  and is
  9.    provided without guarantee or warrantee expressed or implied. This
  10.    program is -not- in the public domain. */
  11.  
  12. /* This example demonstrates how to use glCopyPixels with glPixelZoom
  13.    to zoom up small renderings to fill more pixels. */
  14.  
  15. #include <GL/glut.h>
  16. #include <stdlib.h>
  17.  
  18. void init(void);
  19. void KeyboardFunc(unsigned char key, int x, int y);
  20. void MenuFunc(int value);
  21. void IdleFunc(void);
  22. void ReshapeFunc(int w, int h);
  23. void DisplayFunc(void);
  24.  
  25. struct ProgramState
  26. {
  27.  double w;
  28.  double h;
  29.  double ResolutionX;
  30.  double ResolutionY;
  31.  int shoulder;
  32.  int elbow;
  33.  int wrist;
  34.  double Rotation;
  35.  double RotationIncrement;
  36. };
  37. struct ProgramState ps;
  38.  
  39. void init(void)
  40. {
  41.  glShadeModel (GL_FLAT);
  42.  glDisable(GL_DITHER);
  43.  glClearColor(0.0, 0.0, 0.0, 1.0);
  44.  
  45.  ps.Rotation = 0.0;
  46.  ps.RotationIncrement = 4.0;
  47.  ps.ResolutionX = 1.0;
  48.  ps.ResolutionY = 1.0;
  49.  ps.shoulder = 0.0;
  50.  ps.elbow = 0.0;
  51.  ps.wrist = 0.0;
  52. }
  53.  
  54. void KeyboardFunc (unsigned char key, int x, int y)
  55. {
  56.  switch (key)
  57.  {
  58.  case '1':
  59.   ps.ResolutionX = 1;
  60.   ps.ResolutionY = 1;
  61.   glutPostRedisplay();
  62.   break;
  63.  case '2':
  64.   ps.ResolutionX = 2;
  65.   ps.ResolutionY = 2;
  66.   glutPostRedisplay();
  67.   break;
  68.  case '3':
  69.   ps.ResolutionX = 3;
  70.   ps.ResolutionY = 3;
  71.   glutPostRedisplay();
  72.   break;
  73.  case '4':
  74.   ps.ResolutionX = 4;
  75.   ps.ResolutionY = 4;
  76.   glutPostRedisplay();
  77.   break;
  78.  case '5':
  79.   ps.ResolutionX = 5;
  80.   ps.ResolutionY = 5;
  81.   glutPostRedisplay();
  82.   break;
  83.  case '6':
  84.   ps.ResolutionX = 6;
  85.   ps.ResolutionY = 6;
  86.   glutPostRedisplay();
  87.   break;
  88.  case '7':
  89.   ps.ResolutionX = 7;
  90.   ps.ResolutionY = 7;
  91.   glutPostRedisplay();
  92.   break;
  93.  case '8':
  94.   ps.ResolutionX = 8;
  95.   ps.ResolutionY = 8;
  96.   glutPostRedisplay();
  97.   break;
  98.  case '9':
  99.   ps.ResolutionX = 9;
  100.   ps.ResolutionY = 9;
  101.   glutPostRedisplay();
  102.   break;
  103.  case '0':
  104.   ps.ResolutionX = 10;
  105.   ps.ResolutionY = 10;
  106.   glutPostRedisplay();
  107.   break;
  108.  case '-':
  109.   ps.ResolutionX = 16;
  110.   ps.ResolutionY = 16;
  111.   glutPostRedisplay();
  112.   break;
  113.  case '=':
  114.   ps.ResolutionX = 32;
  115.   ps.ResolutionY = 32;
  116.   glutPostRedisplay();
  117.   break;
  118.  case '\\':
  119.   ps.ResolutionX = 64;
  120.   ps.ResolutionY = 64;
  121.   glutPostRedisplay();
  122.   break;
  123.  case 'o':
  124.   ps.ResolutionX = 1;
  125.   ps.ResolutionY = 8;
  126.   glutPostRedisplay();
  127.   break;
  128.  case 'e':
  129.   ps.ResolutionX = 8;
  130.   ps.ResolutionY = 1;
  131.   glutPostRedisplay();
  132.   break;
  133.  case 'q':
  134.  case 'Q':
  135.  case 27:
  136.   exit(0);
  137.   break;
  138.  default:
  139.   break;
  140.  }
  141. }
  142.  
  143. void MenuFunc(int value)
  144. {
  145.  KeyboardFunc((unsigned char) value, 0, 0);
  146. }
  147.  
  148. void IdleFunc(void)
  149. {
  150.  ps.Rotation += ps.RotationIncrement;
  151.  ps.shoulder = ps.Rotation;
  152.  ps.elbow = ps.Rotation*0.63432;
  153.  ps.wrist = ps.Rotation*0.4313543;
  154.  glutPostRedisplay();
  155. }
  156.  
  157. void ReshapeFunc (int w, int h)
  158. {
  159.  ps.w =w;
  160.  ps.h = h;
  161.  glMatrixMode (GL_PROJECTION);
  162.  glLoadIdentity ();
  163.  gluPerspective(65.0, ps.w/ps.h, 1.0, 20.0);
  164.  glMatrixMode(GL_MODELVIEW);
  165.  glLoadIdentity();
  166.  glTranslatef (0.0, 0.0, -5.0);
  167. }
  168.  
  169. void DisplayFunc(void)
  170. {
  171.  double ForearmLength= 1.2;
  172.  double UpperarmLength =1.0;
  173.  double HandLength = 0.5;
  174.  glViewport (0.0, 0.0, (GLsizei) (ps.w/ps.ResolutionX), (GLsizei) (ps.h/ps.ResolutionY));
  175.  glMatrixMode(GL_MODELVIEW);
  176.  
  177.  glClear (GL_COLOR_BUFFER_BIT);
  178.  glPushMatrix();
  179.  {
  180.   glRotatef (ps.shoulder, 0.0, 0.0, 1.0);
  181.   glTranslatef (0.5*UpperarmLength, 0.0, 0.0);
  182.   glPushMatrix();
  183.   {
  184.    glScalef (UpperarmLength, 0.6, 1.0);
  185.    glutWireCube (1.0);
  186.   }
  187.   glPopMatrix();
  188.   glTranslatef (0.5*UpperarmLength, 0.0, 0.0);
  189.  
  190.   glRotatef (ps.elbow, 0.0, 0.0, 1.0);
  191.   glTranslatef (0.5*ForearmLength, 0.0, 0.0);
  192.   glPushMatrix();
  193.   {
  194.    glScalef (ForearmLength, 0.4, 1.0);
  195.    glutWireCube (1.0);
  196.   }
  197.   glPopMatrix();
  198.   glTranslatef (0.5*ForearmLength, 0.0, 0.0);
  199.  
  200.   glRotatef (ps.wrist, 0.0, 0.0, 1.0);
  201.   glTranslatef (0.5*HandLength, 0.0, 0.0);
  202.   glPushMatrix();
  203.   {
  204.    glScalef (HandLength, 0.2, 1.0);
  205.    glutWireCube (1.0);
  206.   }
  207.   glPopMatrix();
  208.  }
  209.  glPopMatrix();
  210.  
  211.  glPixelZoom(ps.ResolutionX, ps.ResolutionY);
  212.  glCopyPixels(0.0,0.0,
  213.   (GLsizei) (ps.w/ps.ResolutionX), (GLsizei) (ps.h/ps.ResolutionY), GL_COLOR);
  214.  
  215.  glutSwapBuffers();
  216. }
  217.  
  218. void
  219. VisibilityFunc(int vis)
  220. {
  221.   if (vis == GLUT_VISIBLE) {
  222.     glutIdleFunc(IdleFunc);
  223.   } else {
  224.     glutIdleFunc(NULL);
  225.   }
  226. }
  227.  
  228. int main(int argc, char** argv)
  229. {
  230.  glutInit(&argc, argv);
  231.  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  232.  ps.h=500;
  233.  ps.w=500;
  234.  glutInitWindowSize ((int) ps.w, (int) ps.h);
  235.  glutInitWindowPosition (100, 100);
  236.  glutCreateWindow (argv[0]);
  237.  
  238.  init ();
  239.  glutVisibilityFunc(VisibilityFunc);
  240.  glutDisplayFunc(DisplayFunc);
  241.  glutReshapeFunc(ReshapeFunc);
  242.  glutKeyboardFunc(KeyboardFunc);
  243.  glutCreateMenu(MenuFunc);
  244.  glutAddMenuEntry("resolution 1x1", '1');
  245.  glutAddMenuEntry("resolution 2x2", '2');
  246.  glutAddMenuEntry("resolution 3x3", '3');
  247.  glutAddMenuEntry("resolution 4x4", '4');
  248.  glutAddMenuEntry("resolution 5x5", '5');
  249.  glutAddMenuEntry("resolution 6x6", '6');
  250.  glutAddMenuEntry("resolution 7x7", '7');
  251.  glutAddMenuEntry("resolution 8x8", '8');
  252.  glutAddMenuEntry("resolution 9x9", '9');
  253.  glutAddMenuEntry("resolution 10x10", '0');
  254.  glutAddMenuEntry("resolution 16x16", '-');
  255.  glutAddMenuEntry("resolution 32x32", '=');
  256.  glutAddMenuEntry("resolution 64x64", '\\');
  257.  glutAddMenuEntry("resolution 1x8", 'o');
  258.  glutAddMenuEntry("resolution 8x1", 'e');
  259.  glutAttachMenu(GLUT_RIGHT_BUTTON);
  260.  
  261.  glutMainLoop();
  262.  return 0;
  263. }
  264.  
  265.